﻿<html>
    <head>
        <title>
            Médiathèque - Recherche
        </title>
        <?php include('communs/lienCss.php'); ?>
        <script language="Javascript">
            function onclk(){
                if (document.myform.champ.value==""){
                    alert('Veuillez choisir un champs filtre');
                    return false;
                }
                if (document.myform.input.value==""){
                    alert('Veuillez entrer terme-filtre dans le champs de recherche');
                    return false;
                }
                return true;
            }
            function selectByValue(form, str){
                for (i=0; i<form.childElementCount; i++){
                    if (form[i].value == str){
                        form[i].selected = "i";
                    }
                }
            }
        </script>
        <script type="text/javascript" src="js/dropdown/jquery.js"></script>
        <?php 
        function show($id, $table, $order=false){
            if ($table =='piste' or $table =='episode'){
                $sql = 'Select * from '.$table.' where '.$table.'.id='.$id;
            }else{
                $sql = 'Select * from '.$table.' LEFT JOIN collection ON idcoll=collection.id where '.$table.'.id='.$id;
            }
            $result = mysql_query($sql);
            if ($result){
                $row = mysql_fetch_assoc($result);
                echo "<blocimage>\n";
                echo "-- image --";
                echo "</blocimage>\n";
                echo "<blocresult>\n";
                if ($order){
                    echo "<li/>";
                }
                echo "<a href='item.php?no=".$id."'><u>".$row['titre']."</u></a>";
                if (isset($row['anneePublication'])) { 
                    echo " (".$row['anneePublication'].")";
                }
                echo " <br/>\n";
                if (isset($row['sousTitre'])){
                    echo "<subtitle>".$row['sousTitre']."</subtitle><br/>\n";
                }
                echo $table;
                if (isset($row['support'])) { 
                    echo " (".$row['support'].")";
                }elseif ($table=='document' and $row['genre']=='partition'){
                    echo " (partition)";
                }
                echo "<br />\n";
                if (isset($row['duree'])){
                    echo "Duree : " .$row['duree']."<br />\n";
                }
                if ($table =='piste'){
                    $sql2 = 'Select titre from disque where id = '.$row['idDisque'];
                    $result2 = mysql_query($sql2);
                    $row2 = mysql_fetch_assoc($result2);
                    echo "Disque: <a href='recherche.php?mode=piste&champ=Disque&fixed=true&input='".$row2['titre']."&page=1'>".$row2['titre']."</a>\n";
                }elseif ($table =='episode'){
                    $sql2 = 'Select titre from film where id = '.$row['idFilm'];
                    $result2 = mysql_query($sql2);
                    $row2 = mysql_fetch_assoc($result2);
                    echo "Film: <a href='recherche.php?mode=episode&champ=Film&fixed=true&input=".$row2['titre']."&page=1'>".$row2['titre']."</a>\n";
                }elseif (isset($row['nom'])){
                    echo "Collection : <a href='recherche.php?mode=tous&champ=idColl&fixed=true&input=".$row['nom']."&page=1'>".$row['nom']."</a>\n";
                }
                echo "</blocresult>\n";
            }
        }
        function compar($field, $input, $fixed){
        
            if ($field == 'idColl' or $fixed){
                return ''.$field.'=\''.$input.'\'';
            }else{
                return '(lower('.$field.') like "%'.$input.'%" or lower('.$field.') like "'.$input.'" or lower('.$field.') like "%'.$input.'" or lower('.$field.') like "'.$input.'%")';
            }
        }
		function nvl($test, $no){
			if(isset($test)){
				return $no;
			}else{
				return $test;
			}
		}
        ?>
    </head>
    <body>
            <div id="conteneurTete">
                <?php include('communs/enTete.php');?>
            </div> 
 <?php 
            /*include('php/connexionBaseDeDonnees.php');*/
            
            $con = mysql_connect('localhost','root','');

            if (!$con){
                die("Unable to connect to MySQL");
            }

            mysql_select_db('mediatheque');
            
            include('communs/menu.php');
        ?>
        <div id="conteneurCorps">
        
        <form name="myform" method="GET" action="recherche.php">
            <select id="mode" name="mode" onchange="window.location='recherche.php?mode=' + this.value">
                <option disabled="disabled" selected="selected">Sélectionner un support</option>
                <option value="tous">Tous</option>            
                <option value="disque">Élément Audio</option>
                <option value="film">Élément Vidéo</option>
                <option value="piste">Piste</option>
                <option value="episode">Épisode</option>
                <option value="partition">Partition</option>
                <option value="autre">Autre Document</option>    
            </select>
            <script language="Javascript">
                var mod = "<?php if (isset($_GET['mode'])){echo $_GET['mode']; };?>";
                selectByValue(document.myform.mode, mod);                    
            </script>
        <?php
            if (isset($_GET['mode'])){
                echo "<select name='champ'>\n";
                echo '<option disabled="disabled" selected="selected" />';
                switch ($_GET['mode']){
                    case "disque":
                        echo "<option value='Titre'>Titre</option>\n";
                        echo "<option value='Support'>Support</option>\n";
                        echo "<option value='CodeBarre'>CodeBarre</option>\n";
                        echo "<option value='Genre'>Genre</option>\n";
                        echo "<option value='Label'>Étiquette</option>\n";
                        echo "<option value='Annee'>Année de publication</option>\n";
                        echo "<option value='SousTitre'>Sous-Titre</option>\n";
                        echo "<option value='Origine'>Origine</option>\n";
                        echo "<option value='idColl'>Collection</option>\n";
                        echo "<option value='IdArtiste'>Artiste</option>\n";
                        echo "<option value='Notes'>Notes</option>\n";
                        break;
                    case "film":
                        echo "<option value='Titre'>Titre</option>\n";
                        echo "<option value='Support'>Support</option>\n";
                        echo "<option value='CodeBarre'>CodeBarre</option>\n";
                        echo "<option value='Genre'>Genre</option>\n";
                        echo "<option value='Notes'>Notes</option>\n";
                        echo "<option value='Label'>Étiquette</option>\n";
                        echo "<option value='IdActeur'>Acteur</option>\n";
                        echo "<option value='Annee'>Année de publication</option>\n";
                        echo "<option value='SousTitre'>Sous-Titre</option>\n";
                        echo "<option value='Origine'>Origine</option>\n";
                        echo "<option value='idColl'>Collection</option>\n";
                        echo "<option value='Notes'>Notes</option>\n";
                        break;
                    case "piste":
                        echo "<option value='Titre'>Titre</option>\n";
                        echo "<option value='Disque'>Nom du disque</option>\n";
                        echo "<option value='Annee'>Année de publication</option>\n";
                        echo "<option value='idInterprete'>Interprète</option>\n";
                        echo "<option value='Arrangeur'>Arrangeur</option>\n";
                        echo "<option value='Catalogue'>Catalogue</option>\n";
                        echo "<option value='Tonalite'>Tonalité</option>\n";
                        echo "<option value='Compositeur'>Compositeur</option>\n";
                        echo "<option value='idColl'>Collection</option>\n";
                        echo "<option value='Epoque'>Epoque</option>\n";
                        echo "<option value='Forme'>Forme</option>\n";
                        echo "<option value='Genre'>Genre</option>\n";
                        echo "<option value='Instrumentation'>Instrumentation</option>\n";
                        echo "<option value='Orchestrateur'>Orchestrateur</option>\n";
                        echo "<option value='Parolier'>Parolier</option>\n";
                        echo "<option value='Notes'>Notes</option>\n";
                        break;
                    case "episode":
                        echo "<option value='Titre'>Titre</option>\n";
                        echo "<option value='Film'>Nom du film</option>\n";
                        break;
                    case "partition":
                        echo "<option value='Titre'>Titre</option>\n";
                        echo "<option value='Instrument'>Instrument</option>\n";
                        echo "<option value='Arrangeur'>Arrangeur</option>\n";
                        echo "<option value='Catalogue'>Catalogue</option>\n";
                        echo "<option value='idColl'>Collection</option>\n";
                        echo "<option value='Auteur'>Auteur</option>\n";
                        echo "<option value='Genre'>Genre</option>\n";
                        echo "<option value='Notes'>Notes</option>\n";
                        break;
                    case "autre documents":
                        echo "<option value='Titre'>Titre</option>\n";
                        echo "<option value='Type'>Type</option>\n";
                        echo "<option value='Notes'>Notes</option>\n";
                        echo "<option value='Catalogue'>Catalogue</option>\n";
                        echo "<option value='Auteur'>Auteur</option>\n";
                        echo "<option value='idColl'>Collection</option>\n";
                        echo "<option value='Genre'>Genre</option>\n";
                        break;
                    case "tous":
                        echo "<option value='Titre'>Titre</option>\n";
                        echo "<option value='notes'>Notes</option>\n";
                        echo "<option value='idColl'>Collection</option>\n";
                        echo "<option value='Genre'>Genre</option>\n";
                        echo "<option value='Support'>Support</option>\n";
                        break;
                }
                echo "</select>\n";    
                
                if (isset($_GET['input'])){
                    $mod = $_GET['champ'];
                    echo "<script language='Javascript'>\n";
                        echo "var mod = '$mod';\n";
                        echo "selectByValue(document.myform.champ, mod);\n";
                    echo "</script>\n";
                }
                echo "<input name='input' type='text' value='";
                    if (isset($_GET['input'])){
                        echo $_GET['input'];
                    }
                echo "'/>\n";
            }
            
        ?>
        <input name='page' type='hidden' value='1'/>
        <input type='button' value='Rechercher' onclick='if (onclk()) { document.myform.submit()}'/>
        </form>
        <hr />
        
        <?php
            if (isset($_GET['input'])){
                $table = $_GET['mode'];
                $field = $_GET['champ'];
                $input = strtolower($_GET['input']);
                $fixed;
                if (isset($_GET['fixed']) and $_GET['fixed'] = 'true'){
                    $fixed = true;
                }else{
                    $fixed = false;
                }
                $sql = "";
                if ($_GET['champ']=='idColl'){
                    $sql2 = 'Select id from collection where '.compar('nom',$input, $fixed);
                    $result2 = mysql_query($sql2);
                    //echo $sql2."<br/><br/><br/>";
                    if ($result2){
                        $row2 = mysql_fetch_assoc($result2);        
                        $input = $row2['id'];
                    }
                }
				//système de pagination coté prog - Ref>http://antoine-herault.developpez.com/tutoriels/php/pagination-automatique-en-php/
				
				$messagesParPage=10;
				
                if ($_GET['champ']=='idInterprete'){
                    $sql = 'SELECT distinct idPiste as id, \'piste\' as type from interpretePiste INNER JOIN interprete on idPiste=id where '.compar('nom',$input, $fixed);
                }elseif ($_GET['champ']=='IdArtiste'){
                    $sql = 'SELECT distinct idDisque as id, \'disque\' as type from ArtisteDisque INNER JOIN Artiste on idDisque=id where '.compar('nom',$input, $fixed);
                }elseif ($_GET['champ']=='idActeur'){
                    $sql = 'SELECT distinct idfilm as id, \'film\' as type from acteurFilm INNER JOIN acteur on idFilm=id where '.compar('nom',$input, $fixed);
                }elseif ($_GET['champ']=='Disque'){
                    $sql = 'SELECT distinct piste.id, \'piste\' as type from piste INNER JOIN disque on idDisque=disque.id where '.compar('disque.titre',$input, $fixed);
                }elseif ($_GET['champ']=='Film'){
                    $sql = 'SELECT distinct episode.id, \'episode\' as type from episode INNER JOIN film on idFilm=film.id where '.compar('film.titre',$input, $fixed);
                }elseif ($_GET['mode']=='partition'){
                    $sql = 'SELECT distinct id, \'document\' as type from document where genre="partition" and '.compar($field,$input, $fixed);
                }elseif ($_GET['mode']=='autre'){
                    $sql = 'SELECT distinct id, \'document\' as type from document where genre<>"partition" and '.compar($field,$input, $fixed);
                }elseif ($_GET['mode']=='tous'){
                    if ($_GET['champ']=='idColl'){
                        $sql = 'SELECT distinct id, \'document\' as type from document where '.compar($field, $input, $fixed);
                        $sql .= ' UNION SELECT distinct id, \'disque\' as type from disque where '.compar($field, $input, $fixed);
                        $sql .= ' UNION SELECT distinct id, \'film\' as type from film where '.compar($field, $input, $fixed);
                    }else{
                        $sql = 'SELECT distinct id, \'document\' as type from document where '.compar($field, $input, $fixed);
                        $sql .= ' UNION SELECT distinct id, \'disque\' as type from disque where '.compar($field, $input, $fixed);
                        $sql .= ' UNION SELECT distinct id, \'film\' as type from film where '.compar($field, $input, $fixed);
                        $sql .= ' UNION SELECT distinct id, \'piste\' as type from piste where '.compar($field, $input, $fixed);
                        $sql .= ' UNION SELECT distinct id, \'episode\' as type from episode where '.compar($field, $input, $fixed);
                    }
                }else{
                    $sql = 'SELECT id, \''.$table.'\' as type from '.$table.' where '.compar($field, $input, $fixed);
                }
				$total = 0;
				if (mysql_query($sql)){
					$total= mysql_num_rows(mysql_query($sql));
				}
				$nombreDePages=ceil($total/$messagesParPage);

				if(isset($_GET['page']) and !is_null($_GET['page'])){
					$pageActuelle=intval($_GET['page']);
					if($pageActuelle>$nombreDePages){
						$pageActuelle=$nombreDePages;
					}
				}else{
					$pageActuelle=1;  
				}
				
				$start=($pageActuelle-1)*$messagesParPage;
				echo "$total résultat(s) trouvé(s)\n";
				$sql.=' order by 1 LIMIT '.$start.' , '.$messagesParPage;
                $result = mysql_query($sql);
                //echo "$sql <br /><br />\n";
                $l = 'a';
                $ordered= $field=='Disque' or $field=='Film';
                if ($result){
					$fin = $start + $messagesParPage;
					if ($fin > $total) {
						$fin = $total;
					}
					echo "Affichage des résultats ".($start+1)." à ".$fin."\n";
                    if ($ordered) echo "<ol>";
                    while($row = mysql_fetch_assoc($result)){
                        echo "<div class='resultat'>\n";
                        if ($l <> 'a') {
                            echo '<hr class="resultat" />';
                        }
                        show($row['id'], $row['type'], $ordered);
                        echo "</div>\n";
                        $l = $row['id'];
                    }
					if ($ordered) echo "</ol>";
					echo "<br /><br />\n";
					//pagination coté graph
					echo '<br /><br /><p align="center">Page : ';
                }
				for($i=1; $i<=$nombreDePages; $i++){
					if($i==$pageActuelle) {
						echo ' [ '.$i.' ] '; 
					}else{
						echo ' <a href="recherche.php?mode='.$_GET['mode'].'&champ='.$_GET['champ'].'&input='.strtolower($_GET['input']).'&page='.$i.'">'.$i.'</a> ';
					}
				}
				echo '</p>';				
			}
        ?>
        </div> 
        <div id="conteneurPied">
            <?php include('communs/piedDePage.php'); ?>
        </div>
    </body>
</html>